package com.woniuxy.test05;

/**
 * @Author author
 * @Create 2023-09-19 9:47
 */
public class Singleton {
    //定义一个static本类对象作为自己成员变量
    private static volatile Singleton singleton;

    //构造私有
    private Singleton(){
    }
    //多线程环境有问题--同步锁，性能差
    //static
    public  static Singleton createInstance1(){
        synchronized (Singleton.class){
            if(singleton==null){
                singleton=new Singleton();
            }
        }
        return singleton;
    }


    //double checked双检锁
    public  static Singleton createInstance(){
        if(singleton==null){
            synchronized (Singleton.class){
                if(singleton==null){
                    singleton=new Singleton();
                }
            }
        }
        return singleton;
    }

}
